package mya_dc.compilation_server.gui;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import mya_dc.compilation_server.users.UserProjectManager;
import mya_dc.compilation_server.database.*;

public class UsersProjectsList implements TableModel
{
	private static String[] ColumnName = {"Project","User Name","Size (Bytes)", "Last Access"};
	
	public UsersProjectsList (UserProjectManager userProjectManager)
	{
		m_UserProjectManager = userProjectManager;
	}
		
	@Override
	public void addTableModelListener(TableModelListener l) {}

	@Override
	public Class<?> getColumnClass(int columnIndex) 
	{
		return getValueAt(0, columnIndex).getClass();
	}

	@Override
	public int getColumnCount() 
	{
		return ColumnName.length;
	}

	
	@Override
	public String getColumnName(int columnIndex)
	{
		return ColumnName[columnIndex];
	}

	@Override
	public int getRowCount() 
	{
		return getCountRowSync();
	}
	
	public synchronized int getCountRowSync()
	{
		return DatabaseManager.countProjects();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) 
	{
		return getValueAtSync(rowIndex,columnIndex);
	}
	

	public synchronized Object getValueAtSync(int rowIndex, int columnIndex) 
	{
		Object[] CurrRow = DatabaseManager.getProjectTableRow(rowIndex + 1);
		if (CurrRow == null)
		{
			return null;
		}
		if (columnIndex != 3)
		{
			return CurrRow[columnIndex];
		}
		else
		{
			if (m_UserProjectManager.isUserConnected(CurrRow[1].toString(),CurrRow[0].toString()))
			{
					return "ACTIVE NOW";
			}
			else 
			{
					return CurrRow[columnIndex].toString();
			}
		}
	}
	
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) 
	{
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {}
	
	private UserProjectManager m_UserProjectManager = null;
}
